const fs = require('fs')
// 创建了一个可读流
// 读小文件 fs.readFileSync()
const readStream = fs.createReadStream('./a.txt', {
    highWaterMark: 32 * 1024 // 单位是字节
})
const writeStream = fs.createWriteStream('./b.txt')

// 监听文件打开事件
readStream.on('open', _ => {
    console.log('文件已打开')
})
// 文件已准备好事件
readStream.on('ready', _ => {
    console.log('文件已准备好')
})
// 读取过程事件
readStream.on('data', chunk => {
    console.log('开始读数据,已读数据字节大小：', readStream.bytesRead)
    writeStream.write(chunk)
})

// 读取结束，自动关闭事件
readStream.on('close', _=>{
    console.log('文件已关闭')
})

writeStream.on('open', _ => {
    console.log('文件已打开')
})
writeStream.on('ready', _ => {
    console.log('文件已准备好，可以开始写入了')
    writeStream.write('hello world')
})
writeStream.on('close', _=>{
    console.log('文件关闭')
})
// 关闭
writeStream.close()


